/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.dishes.impl;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.qinyeit.serviceapp.entity.Dishes;
import com.qinyeit.serviceapp.entity.DishesCategory;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyeit.serviceapp.service.branchs.BranchDataServiceImpl;
import com.qinyeit.serviceapp.service.dishes.DishesCategoryService;
import com.qinyeit.serviceapp.service.dishes.DishesService;
import com.qinyeit.serviceapp.service.organization.BranchService;
import com.qinyeit.serviceapp.utils.SearchUtils;
import com.qinyetech.springstage.core.entity.search.Searchable;
import com.qinyetech.springstage.core.lang.QyMap;
import com.qinyetech.springstage.core.lang.StringUtils;
import com.qinyetech.springstage.core.repository.jdbc.PageNamedParameterJdbcTemplate;
import com.qinyetech.springstage.core.utils.JSONUtils;
import org.apache.commons.collections.CollectionUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyeit.serviceapp.entity.DishesStore;
import com.qinyeit.serviceapp.repository.dishes.DishesStoreRepository;
import com.qinyeit.serviceapp.service.dishes.DishesStoreService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/** 
 * ClassName: DishesStoreServiceImpl <br/> 
 * Function: 菜品库 业务实现 <br/> 
 * date: 2018-11-23 11:04:09 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class DishesStoreServiceImpl extends BaseService<DishesStore, Long> implements DishesStoreService {


	@Autowired
	private DishesStoreRepository dishesStoreRepository;
	@Autowired
	private DishesCategoryService dishesCategoryService;
	@Autowired
	private DishesService dishesService;
	@Autowired
	private PageNamedParameterJdbcTemplate jdbcTemplate;
	@Autowired
	private BranchDataServiceImpl branchDataService;

	@Override
	public List<DishesCategory> getDishesCategorys(Long storeId,boolean isNew,Long branchId) {
		Searchable searchable = Searchable.newSearchable();
		searchable.addSearchFilter(SearchUtils.getDeletedFilter());
		searchable.addSearchParam("storeId_eq", storeId);
		List<DishesCategory> categoryList = dishesCategoryService.findWithSort(searchable);

		List<String> stringList = Lists.newArrayList();
		if (isNew){
			log.info("branchId : {}",branchId);
			stringList = branchDataService.getUseDishesUUids(branchId);
		}

		List<Dishes> dishesList = dishesService.findWithSort(searchable);

		List<Dishes> dishesListItem = null;
		if(CollectionUtils.isNotEmpty(categoryList)) {
			for (DishesCategory category : categoryList) {
				dishesListItem = Lists.newArrayList();
				for (Dishes dishes : dishesList) {
					if (dishes.getCategory().getId()==category.getId()){
						log.info("xxxxx1111:{}",isNew);
						log.info("xxxxx2222:{}",stringList);
						if(isNew && CollectionUtils.isNotEmpty(stringList) && stringList.contains(dishes.getUuid())){
							continue;
						}
						dishesListItem.add(dishes);
					}
				}
				category.setDishess((JSONArray) JSONUtils.include(dishesListItem,"id","name","spell"));
			}
		}
		
		/*if(CollectionUtils.isNotEmpty(categoryList)){
			for (DishesCategory category : categoryList) {
				searchable.addSearchParam("category.id_eq",category.getId());
				if (isNew){
//					searchable.addSearchParam("hasIssue_eq",false);



				}
				category.setDishess((JSONArray) JSONUtils.include(dishesService.findWithSort(searchable),"id","name","spell"));
			}
		}*/
		categoryList = categoryList.stream().filter(x->CollectionUtils.isNotEmpty(x.getDishess())).collect(Collectors.toList());
		return categoryList;
	}

	@Override
	public Page<Map<String,Object>> findAllForPage(Searchable searchable,Long merchantGroupId) {

		String sql = "SELECT s.*,t1.dishes_count, t2.pack_count,t3.material_count from dishes_stores s\n" +
				"LEFT JOIN (\n" +
				"\tselect store_id, count(id) as dishes_count from dishess d where d.type='DISHES' and d.is_deleted=0 group by store_id\n" +
				") t1  on t1.store_id=s.id\n" +
				"LEFT JOIN (\n" +
				"\tselect store_id, count(id) as pack_count from dishess d where d.type='PACKAGE' and d.is_deleted=0 group by store_id\n" +
				") t2  on t2.store_id=s.id\n" +
				"LEFT JOIN (\n" +
				"\tselect store_id, count(id) as material_count from materials m where m.material_type != 'PRODUCT' and m.is_deleted=0 group by store_id\n" +
				") t3  on t3.store_id=s.id \n" +
				"where merchant_group_id = :merchant_group_id and s.is_deleted = 0";

		QyMap param = QyMap.map("merchant_group_id",merchantGroupId);

		String industry = searchable.getValue("industry_eq");
		if(StringUtils.isNotBlank(industry)){
			if(industry.equals("ZC")){
				sql += " and (s.industry='ZC' or s.industry='KC' )";
			}else {
				sql += " and s.industry=:industry";
				param.setv("industry",industry);
			}
		}
//

		Page<Map<String,Object>> listPage = jdbcTemplate.queryForList(sql,param,searchable.getPage());

		return listPage;
	}
}